/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package comandaXM.utils.format;


import javax.swing.JSplitPane;
import javax.swing.JTable;
import comandaXM.excepcions.ExceptionXML;  

/**
 *
 * @author xmarti
 */
public class FormatJTableAndJSplitPane {
    private FormatJSplitPane data;
    private JSplitPane jSplitPane;
    private JTable jTable;
    private String path;
    public FormatJTableAndJSplitPane(){
        
    }
    public FormatJTableAndJSplitPane(JSplitPane jSplitPane, JTable jTable, String path) throws ExceptionXML {        
            Format format= new Format();
            this.jSplitPane=jSplitPane;
            this.jTable=jTable;
            this.path=path;            
            this.data= new FormatJSplitPane();
        try {
            
            this.data= format.read (path+".XML");
        } catch (ExceptionXML ex) {            
            this.setFormat();
            this.saveFormat();
        }
       
    }       
    public void chargeFormat(){
         for (int i =0; i< this.data.getCols().size();i++){
            jTable.getColumnModel().getColumn(i).setPreferredWidth(data.getCol(i));                                                   
        }
        jSplitPane.setDividerLocation(data.getResizeWeight());     
    }
    public void setFormat(){              
       this.data.resetCols();
       for (int i =0; i< jTable.getColumnModel().getColumnCount();i++){
           this.data.setCol(jTable.getColumnModel().getColumn(i).getWidth());
       }
       if(jSplitPane.getDividerLocation()<400){
           this.data.setResizeWeight(jSplitPane.getDividerLocation());
       }       
    }
    public void saveFormat() throws ExceptionXML {
        Format format= new Format();         
        format.write(path+".XML", this.data);       
    }    
}
